package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.ydkj.a37e_mall.Classes.Applicatiom.MyApplication;
import com.ydkj.a37e_mall.Classes.Model.Request.DebitcardReq;
import com.ydkj.a37e_mall.Classes.Model.Request.DeletebandcardReq;
import com.ydkj.a37e_mall.Classes.Model.Response.DebitcardResp;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.Async_http_get;
import com.ydkj.a37e_mall.Resource.Utils.CommonUtil;
import com.ydkj.a37e_mall.Resource.Utils.Const;
import com.ydkj.a37e_mall.Resource.Utils.GsonImpl;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;
import com.ydkj.a37e_mall.Resource.View.CustomProgressDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.ydkj.a37e_mall.R.mipmap.my;

/**
 * Created by Administrator on 2017/1/12.
 */

public class MyBankcardActivity extends BaseActivity implements View.OnClickListener {
    private Activity mActivity;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private MyApplication myApplication;
    private ListView lv_my_bankcard;
    private RelativeLayout rly_my_add_bankcard;
    private List<DebitcardResp.ResultEntity.DataEntity> stringList;
    private MyBankcarAdapter myBankcarAdapter;
    private ImageView img_back;
    private TextView tv_edit, tv_delete;
    private static final int LOAD = 0;// 加载
    private static final int REFRESH = 1;// 刷新
    private static final int MORE = 2;// 更多
    private int page;
    private boolean is_edit = false;
    private String id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mybankcardlayout);
        mActivity = this;
        init();

    }

    private void init() {
        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(mActivity);
        myApplication = MyApplication.getInstance();
        rly_my_add_bankcard = (RelativeLayout) mActivity.findViewById(R.id.rly_my_add_bankcard);
        img_back = (ImageView) mActivity.findViewById(R.id.img_back);
        tv_edit = (TextView) mActivity.findViewById(R.id.tv_edit);
        tv_delete = (TextView) mActivity.findViewById(R.id.tv_delete);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        stringList = new ArrayList<DebitcardResp.ResultEntity.DataEntity>();


        myBankcarAdapter = new MyBankcarAdapter(stringList);
        lv_my_bankcard = (ListView) mActivity.findViewById(R.id.lv_my_bankcard);

        lv_my_bankcard.setAdapter(myBankcarAdapter);


        rly_my_add_bankcard.setOnClickListener(this);
        tv_edit.setOnClickListener(this);
        tv_delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rly_my_add_bankcard:
                Intent intent = new Intent(mActivity, AddBankcardActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_edit:
                if (is_edit == false) {
                    tv_edit.setText("完成");

                    tv_delete.setVisibility(View.VISIBLE);
                    is_edit = true;
                } else {
                    tv_edit.setText("编辑");
                    tv_delete.setVisibility(View.GONE);
                    is_edit = false;
                }
                myBankcarAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_delete:
                Deletebandcard();
                break;
        }
    }

    private class MyBankcarAdapter extends BaseAdapter {
        private List<DebitcardResp.ResultEntity.DataEntity> stringList;

        private MyBankcarAdapter(List<DebitcardResp.ResultEntity.DataEntity> info) {
            if (info == null) {
                info = new ArrayList<DebitcardResp.ResultEntity.DataEntity>();
            }
            stringList = info;
        }

        @Override
        public int getCount() {
            return stringList.size();
        }

        @Override
        public DebitcardResp.ResultEntity.DataEntity getItem(int position) {
            return stringList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup viewGroup) {
            ViewHolder vh = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.mybankcard_list, null);
                vh = new ViewHolder();
                vh.img_bankcard_list_icon = (ImageView) convertView.findViewById(R.id.img_bankcard_list_icon);
                vh.img_bankcard_list_select = (ImageView) convertView.findViewById(R.id.img_bankcard_list_select);

                vh.tv_bankcard_list_name = (TextView) convertView.findViewById(R.id.tv_bankcard_list_name);
                vh.tv_bankcard_list_num = (TextView) convertView.findViewById(R.id.tv_bankcard_list_num);
                vh.cb_choose = (ImageView) convertView.findViewById(R.id.cb_choose);
                vh.lly_check = (LinearLayout) convertView.findViewById(R.id.lly_check);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            convertView.setTag(vh);

            vh.img_bankcard_list_select.setVisibility(View.GONE);
            vh.tv_bankcard_list_name.setText(getItem(position).getBank());
            String str = getItem(position).getAccount().substring(getItem(position).getAccount().length() - 4, getItem(position).getAccount().length());
            vh.tv_bankcard_list_num.setText(str);

            //编辑
            if (is_edit == true) {
                vh.cb_choose.setVisibility(View.VISIBLE);
                tv_edit.setText("完成");

            } else {
                vh.cb_choose.setVisibility(View.GONE);
                tv_edit.setText("编辑");
            }

            if (getItem(position).getBank().equals("北京银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.beijingbank));
            }
            if (getItem(position).getBank().equals("储蓄银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.chuxubank));
            }
            if (getItem(position).getBank().equals("工商银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.gongshangbank));
            }
            if (getItem(position).getBank().equals("广发银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.guangfabank));
            }
            if (getItem(position).getBank().equals("华夏银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.huaxiabank));
            }
            if (getItem(position).getBank().equals("建设银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.jianshebank));
            }
            if (getItem(position).getBank().equals("交通银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.jiaotongbank));
            }
            if (getItem(position).getBank().equals("民生银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.minshengbank));
            }
            if (getItem(position).getBank().equals("平安银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.pinganbank));
            }
            if (getItem(position).getBank().equals("浦发银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.pufabank));
            }
            if (getItem(position).getBank().equals("上海银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.shanghaibank));
            }
            if (getItem(position).getBank().equals("兴业银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.xingyebank));
            }
            if (getItem(position).getBank().equals("招商银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.zhaoshangbank));
            }
            if (getItem(position).getBank().equals("中国银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.zhongguobank));
            }
            if (getItem(position).getBank().equals("中兴银行")) {
                vh.img_bankcard_list_icon.setBackground(getResources().getDrawable(R.mipmap.zhongxinbank));
            }


            vh.lly_check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (is_edit == true) {
                        Log.e("ssss", "1111");
                        DebitcardResp.ResultEntity.DataEntity info = stringList.get(position);
                        for (DebitcardResp.ResultEntity.DataEntity infos : stringList) {
                            if (infos.getId().equals(info.getId())) {
                                infos.setCheick(true);
                            } else {
                                infos.setCheick(false);
                            }
                            myBankcarAdapter.notifyDataSetChanged();
                        }
                    }
                }
            });


            DebitcardResp.ResultEntity.DataEntity info = stringList.get(position);
            if (info.isCheick()) {
//                vh.img_bankcard_list_select.setVisibility(View.VISIBLE);

                vh.cb_choose.setBackground(getResources().getDrawable(R.mipmap.choose_red));

            } else {
                vh.cb_choose.setBackground(getResources().getDrawable(R.mipmap.choose_default));

            }

            return convertView;
        }

        private class ViewHolder {
            private ImageView img_bankcard_list_icon, img_bankcard_list_select, cb_choose;
            private TextView tv_bankcard_list_name, tv_bankcard_list_num;
            private LinearLayout lly_check;
        }
    }


    private void Getdebitcard(final int type) {
        CustomProgressDialog.createDialog(mActivity).show();
        DebitcardReq req = new DebitcardReq();
        req.setUuid(myApplication.getDataEntity().getUuid());
        if (type == LOAD || type == REFRESH) {
            page = 1;
        } else {
//            page = hotProductInfoList.size() / 10 + 1;
        }
        req.setPage(page + "");
        req.setPage_size("10");
        RequestParams params = new RequestParams();
        params.put("token", sharedPreferencesUtil.getAccessToken());
        params.put("secret", req.getSecret());
        Async_http_get.get(Const.COMMON_ADDRESS + req.urlString(), params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.e("url", "hotProductList_json==" + response);
                CustomProgressDialog.Dismiss();
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                if (errmsg.equals("暂无银行卡")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    stringList.clear();
                    myBankcarAdapter.notifyDataSetChanged();
                    return;
                }
                if (errno.equals("000")) {
                    DebitcardResp resp = GsonImpl.get().toObject(response.toString(), DebitcardResp.class);
                    if (type == LOAD || type == REFRESH) {
                        stringList.clear();
                        stringList.addAll(resp.getResult().getData());
                    } else {
                        stringList.addAll(resp.getResult().getData());
                    }


                    myBankcarAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }

    private void Deletebandcard() {

        for (DebitcardResp.ResultEntity.DataEntity dataEntity : stringList) {
            if (dataEntity.isCheick() == true) {
                id = dataEntity.getId();
            }
        }
        if (id.equals("")) {
            return;
        }
        Log.e("ssss", "银行卡id==>" + id);
        CustomProgressDialog.createDialog(mActivity).show();
        DeletebandcardReq req = new DeletebandcardReq();
        req.setUuid(myApplication.getDataEntity().getUuid());
        RequestParams params = new RequestParams();
        params.put("token", sharedPreferencesUtil.getAccessToken());
        params.put("secret", req.getSecret());
        AsyncHttpClient httpClient = new AsyncHttpClient();
        httpClient.put(Const.COMMON_ADDRESS + req.urlString() + id, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                if (errno.equals("000")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    Getdebitcard(REFRESH);

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Getdebitcard(REFRESH);
    }
}
